package com.longrise.co.ui.carlife.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.util.PrefUtils;
import com.longrise.co.R;
import com.longrise.co.ui.carlife.contract.CarLifeContract;
import com.longrise.co.ui.carlife.model.CarLifeModel;
import com.longrise.co.ui.carlife.model.YearCheckBean;
import com.longrise.co.ui.carlife.presenter.CarLifePresenter;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;

import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-24.
 */

public class YearCheckFragment extends BaseFragment<CarLifePresenter, CarLifeModel> implements CarLifeContract.View<YearCheckBean> {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.carinspect_tv_carno)
    TextView carinspectTvCarno;
    @Bind(R.id.carinspect_tv_day)
    TextView carinspectTvDay;
    @Bind(R.id.carinspect_tv_info)
    TextView carinspectTvInfo;
    @Bind(R.id.carinspect_btn)
    TextView carinspectBtn;
    @Bind(R.id.carinspect_layout)
    LinearLayout infoLayout;
    @Bind(R.id.carinspect_tv_refresh)
    TextView refreshTV;

    private String mCarNo ;

    private String mDate;
    private YearCheckBean mYearCheck;
    private String mCarTypeCode;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_car_inspect;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        mCarNo = getArguments().getString("carNo");
        mDate = getArguments().getString("createDate");
        mCarTypeCode = getArguments().getString("carTypeCode");

        setText(headTitle, "年检提醒");
        setText(carinspectTvCarno, mCarNo);

        long daytime = PrefUtils.getLong(getContext(), mCarNo, -1);
        if (daytime > 0) {//已设置提醒
            carinspectBtn.setText("已设置提醒");
        }
        mPresenter.getYearCheckRequest(mCarNo, mDate ,mCarTypeCode);
    }


    @Override
    public void requestSuccess(YearCheckBean yearCheckBean) {
        mYearCheck = yearCheckBean;
        String tip ;
        if(yearCheckBean != null && !StringUtils.isEmpty(yearCheckBean.getDays())) {
            int days = Integer.parseInt(yearCheckBean.getDays());
            if(days > 0) {
                setText(carinspectTvDay, yearCheckBean.getDays());
                tip = "您在" + days + "天后，可申请年检标！";
            } else {
                tip = "可申领年检标";
            }
            setText(carinspectTvInfo, tip);


            if ("1".equals(yearCheckBean.getType())) {//距离年检开始的时间
                infoLayout.setBackgroundResource(R.drawable.circle_bg);
                carinspectTvDay.setTextColor(Color.parseColor("#fe5c14"));
                refreshTV.setTextColor(Color.parseColor("#fe5c14"));
            } else if ("2".equals(yearCheckBean.getType())) {//年检快要结束的时间
                infoLayout.setBackgroundResource(R.drawable.circle_other_bg);
                carinspectTvDay.setTextColor(Color.parseColor("#ffb10b"));
                refreshTV.setTextColor(Color.parseColor("#ffb10b"));
                carinspectBtn.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onComplete() {

    }

    public static YearCheckFragment newInstance(String carNo, String createDate ,String carTypeCode) {

        Bundle args = new Bundle();
        args.putString("carNo", carNo);
        args.putString("createDate", createDate);
        args.putString("carTypeCode" ,carTypeCode);
        YearCheckFragment fragment = new YearCheckFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick({R.id.head_back, R.id.carinspect_tv_refresh, R.id.carinspect_tv_rule, R.id.carinspect_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                _mActivity.onBackPressed();
                break;
            case R.id.carinspect_tv_refresh:
                mPresenter.getYearCheckRequest(mCarNo, mDate, mCarTypeCode);
                break;
            case R.id.carinspect_tv_rule:
                start(InspectRuleFragment.newInstance());
                break;
            case R.id.carinspect_btn:
                setAlarm();
                break;
        }
    }

    //设置定时闹钟提示年检时间
    private void setAlarm() {
        try {
            long daytimes = PrefUtils.getLong(getContext(), mCarNo, -1);
            if (daytimes > 0) {
                PrefUtils.setLong(getContext(), mCarNo, -1);
                carinspectBtn.setText("提醒我");
                showLongToast("取消提醒成功");
            } else {
                PrefUtils.setLong(getContext(), mCarNo,getDistanceDate(Integer.valueOf(mYearCheck.getDays())));
                carinspectBtn.setText("已设置提醒");
                showLongToast("设置提醒成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取多少天之后的日期
     */
    public static long getDistanceDate(int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, days);
        return c.getTime().getTime();
    }
}
